package com.journaldev.spring;

import java.sql.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.journaldev.spring.model.City;
import com.journaldev.spring.model.Patient;
import com.journaldev.spring.model.UserInfo;
import com.journaldev.spring.model.Ward;
import com.journaldev.spring.service.CityService;
import com.journaldev.spring.service.DistrictService;
import com.journaldev.spring.service.PatientService;
import com.journaldev.spring.service.UserInfoService;
import com.journaldev.spring.service.WardService;
import com.journaldev.spring.util.SimpleCityEditor;
import com.journaldev.spring.util.SimpleDateTimeEditor;
import com.journaldev.spring.util.SimpleUserEditor;
import com.journaldev.spring.util.SimpleWardEditor;


@Controller
public class PatientController {
	
	private PatientService patientService;
	
	private UserInfoService userInfoService;
	
	private WardService wardService;
	
	private DistrictService districtService;
	
	private CityService cityService;
	
	@Autowired(required=true)
	@Qualifier(value="wardService")
	public void setWardService(WardService wardService) {
		this.wardService = wardService;
	}
	
	@Autowired(required=true)
	@Qualifier(value="districtService")
	public void setDistrictService(DistrictService districtService) {
		this.districtService = districtService;
	}
		
	@Autowired(required=true)
	@Qualifier(value="cityService")
	public void setCityService(CityService ps){
		this.cityService = ps;
	}

	@InitBinder
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
        binder.registerCustomEditor(Date.class, new SimpleDateTimeEditor());
        binder.registerCustomEditor(UserInfo.class, new SimpleUserEditor(userInfoService));
        binder.registerCustomEditor(Ward.class, new SimpleWardEditor(wardService));
    }
	
	@Autowired(required=true)
	@Qualifier(value="userInfoService")
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	@Autowired(required=true)
	@Qualifier(value="patientService")
	public void setPatientService(PatientService patientService) {
		this.patientService = patientService;
	}
	
	@RequestMapping(value = {"/patients"}, method = RequestMethod.GET)
	public String listPatients(HttpServletRequest request,Model model) {
		model.addAttribute("patient", new Patient());
		UserInfo user = (UserInfo) request.getSession().getAttribute("USER_LOGIN");
		model.addAttribute("listPatients", this.userInfoService.getListPatient(user.getId()));
		model.addAttribute("listWards", this.wardService.listWard());
		model.addAttribute("listDistricts", this.districtService.listDistrict());
		model.addAttribute("listcities", this.cityService.listCity());
		model.addAttribute("id_user", user.getId());
		return "patient";
	}
	
	@RequestMapping(value = {"/patient/add"}, method = RequestMethod.POST)
	public String addPatient(@ModelAttribute("patient") Patient p,  BindingResult result, Model model, RedirectAttributes redirectAttrs) {
		if(result.hasErrors()) {
			model.addAttribute("message", "add errors");
			return "redirect:/patients";
		}
		if(p.getId() != 0) {
			this.patientService.updatePatient(p);
			redirectAttrs.addFlashAttribute("message", "update success id =" + p.getId());
		} else {
			this.patientService.addPatient(p);
			redirectAttrs.addFlashAttribute("message", "add success");
		}
		model.asMap().clear();
		return "redirect:/patients";
	}
	
	@RequestMapping("/patient/remove/{id}")
    public String removePatient(@PathVariable("id") int id){
		
        this.patientService.removePatient(id);
        return "redirect:/patients";
    }
 
    @RequestMapping("/patient/edit/{id}")
    public String editPatient(@PathVariable("id") int id, Model model, HttpServletRequest request){
        model.addAttribute("patient", this.patientService.getPatientById(id));
        UserInfo user = (UserInfo) request.getSession().getAttribute("USER_LOGIN");
		model.addAttribute("listPatients", this.userInfoService.getListPatient(user.getId()));
		model.addAttribute("listWards", this.wardService.listWard());
		model.addAttribute("listDistricts", this.districtService.listDistrict());
		model.addAttribute("listcities", this.cityService.listCity());
		model.addAttribute("id_user", user.getId());
		return "patient";
    }

}
